//package dev2dev.textclient.voip;
//
///**
// * @author jinbing.yao
// * @createTime 2023-10-14 16:15
// **/
//import javax.sound.sampled.*;
//import java.io.IOException;
//import java.net.DatagramPacket;
//import java.net.DatagramSocket;
//import java.net.InetAddress;
//import java.nio.ByteBuffer;
//import java.util.Properties;
//import javax.sip.*;
//import javax.sip.address.*;
//import javax.sip.header.*;
//import javax.sip.message.*;
//
//public class SipAudioReceiver2 implements SipListener {
//
//    private static final int SAMPLE_RATE = 8000; // 采样率
//    private static final int BUFFER_SIZE = 1024; // 缓冲区大小
//
//    private SipFactory sipFactory;
//    private SipStack sipStack;
//    private SipProvider sipProvider;
//    private MessageFactory messageFactory;
//    private HeaderFactory headerFactory;
//    private AddressFactory addressFactory;
//    private ListeningPoint listeningPoint;
//    private String ip;
//    private int port;
//
//    private AudioFormat audioFormat;
//    private DataLine.Info dataLineInfo;
//    private SourceDataLine sourceDataLine;
//
//
//    public SipAudioReceiver2(String ip, int port) {
//        this.ip = ip;
//        this.port = port;
//    }
//
//    public void init() throws Exception {
//        sipFactory = SipFactory.getInstance();
//        sipFactory.setPathName("gov.nist");
//
//        audioFormat = new AudioFormat(SAMPLE_RATE, 16, 1, true, true);
//        dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
//        sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
//        sourceDataLine.open(audioFormat);
//        sourceDataLine.start();
//
//        Properties properties = new Properties();
//        properties.setProperty("javax.sip.STACK_NAME", "SipHandler");
//        properties.setProperty("javax.sip.IP_ADDRESS", "192.168.31.101");
//        properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
//        properties.setProperty("gov.nist.javax.sip.SERVER_LOG", "SipHandler.txt");
//        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", "SipHandlerDebug.log");
//        sipStack = sipFactory.createSipStack(properties);
//
//        headerFactory = sipFactory.createHeaderFactory();
//        addressFactory = sipFactory.createAddressFactory();
//        messageFactory = sipFactory.createMessageFactory();
//        listeningPoint = sipStack.createListeningPoint(ip, port, "udp");
//        sipProvider = sipStack.createSipProvider(listeningPoint);
//        sipProvider.addSipListener(this);
//    }
//
//    public void processRequest(RequestEvent requestEvent) {
//        // 处理收到的请求
//        System.out.println("processRequest");
//        processInvite(requestEvent);
//    }
//
//    public void processResponse(ResponseEvent responseEvent) {
//        // 处理收到的响应
//        System.out.println("processResponse");
//    }
//
//    public void processTimeout(TimeoutEvent timeoutEvent) {
//        // 处理超时事件
//        System.out.println("processTimeout");
//    }
//
//    public void processIOException(IOExceptionEvent exceptionEvent) {
//        // 处理I/O异常
//        System.out.println("processIOException");
//    }
//
//    public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
//        // 处理事务终止事件
//        System.out.println("processTransactionTerminated");
//    }
//
//    public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
//        // 处理对话终止事件
//        System.out.println("processDialogTerminated");
//    }
//
//    public void processInvite(RequestEvent requestEvent) {
//        // 处理收到的INVITE请求
//        try {
//            System.out.println("111111");
//            // 获取音频输出设备
//
//
//            // 获取INVITE请求的SDP信息
//            Request request = requestEvent.getRequest();
//            byte[] sdpBytes = request.getRawContent();
//            String sdp = new String(sdpBytes);
//
//            // 创建200 OK响应
//            Response response = messageFactory.createResponse(200, request);
//            response.setContent(sdpBytes, headerFactory.createContentTypeHeader("application", "sdp"));
//
//
////            ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
////            toHeader.setTag("888"); //This is mandatory as per the spec.
//
//            // 发送200 OK响应
//            ServerTransaction serverTransaction = requestEvent.getServerTransaction();
////            ServerTransaction serverTransaction = sipProvider.getNewServerTransaction(request);
////            serverTransaction.sendResponse(response);
//
//            // 解析SDP信息获取对方的IP地址和端口
//            String[] lines = sdp.split("\r\n");
//            String remoteHost = null;
//            int remotePort = 0;
//            for (String line : lines) {
//                if (line.startsWith("c=IN IP4")) {
//                    remoteHost = line.substring(9);
//                } else if (line.startsWith("m=audio")) {
//                    String[] parts = line.split(" ");
//                    remotePort = Integer.parseInt(parts[1]);
//                }
//            }
//
//            // 创建对方的InetAddress对象
//            InetAddress remoteAddress = InetAddress.getByName(remoteHost);
//
//            // 创建UDP套接字
////            DatagramSocket datagramSocket = new DatagramSocket(port);
//
//            // 创建音频输出线程
//
//
//                    // 接收音频数据并播放
//            byte[] buffer = new byte[BUFFER_SIZE];
//            buffer=  (byte[]) request.getContent();
//
//    //                        DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
//    //                        datagramSocket.receive(datagramPacket);
//            sourceDataLine.write(buffer, 0, buffer.length);
//
//    //                        sourceDataLine.write(datagramPacket.getData(), 0, datagramPacket.getLength());
//
//
//
//
//
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    public void processBye(RequestEvent requestEvent) {
//        System.out.println("1111");
//        // 处理收到的BYE请求
//    }
//
//    public void processAck(RequestEvent requestEvent) {
//        System.out.println("2222");
//        // 处理收到的ACK请求
//    }
//
//    public void processCancel(RequestEvent requestEvent) {
//        System.out.println("3333");
//        // 处理收到的CANCEL请求
//    }
//
//    public void processRegister(RequestEvent requestEvent) {
//        System.out.println("4444");
//        // 处理收到的REGISTER请求
//    }
//
//    public void processOptions(RequestEvent requestEvent) {
//        // 处理收到的OPTIONS请求
//    }
//
//    public static void main(String[] args) {
//        try {
////            SipAudioReceiver sipAudioReceiver = new SipAudioReceiver("192.168.31.101", 5060);
////            sipAudioReceiver.init();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
